package com.leejxx.health.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.common.RedisConst;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.entity.Result;
import com.leejxx.health.pojo.CheckGroup;
import com.leejxx.health.pojo.Setmeal;
import com.leejxx.health.service.CheckMealService;
import com.leejxx.health.utils.AliYunOssUtils;
import com.leejxx.health.vo.AddOrEditCheckMealParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @Author LeeJx
 * @ClassName CheckMealController
 * @Description TODO
 * @date 2021/1/2 9:20
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/checkMeal")
public class CheckMealController {
    @DubboReference
    private CheckMealService checkMealService;
//    @Autowired
//    private QiniuUtils qiniuUtils;
    @Autowired
    private AliYunOssUtils aliYunOssUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("[检查套餐管理-分页查询]data:{}", queryPageBean);
        PageResult pageResult = checkMealService.findByPage(queryPageBean);
        log.info("[检查套餐管理-分页查询成功]data:{}", pageResult);
        return new Result(true, MessageConst.QUERY_SETMEALLIST_SUCCESS, pageResult);
    }

    @RequestMapping("/findAllCheckGroups")
    public Result findAllCheckGroups() {
        log.info("[检查套餐管理-新增套餐检查组回显]");
        List<CheckGroup> checkGroups = checkMealService.findAllCheckGroups();
        log.info("[检查套餐管理-新增套餐检查组回显成功]data:{}", checkGroups);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroups);
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        log.info("文件上传，name:{},size:{}", multipartFile.getOriginalFilename(), multipartFile.getSize());
        //原始文件名
        String originalFileName = multipartFile.getOriginalFilename();
        //获取当前时间戳
        long currentTimeMillis = System.currentTimeMillis();
        //使用UUID构造不重复的文件名
        String fileName = currentTimeMillis + "_" + UUID.randomUUID().toString().replace("-", "") + "_" + originalFileName;

        String url = null;
        //获取输入流并上传
        try (InputStream is = multipartFile.getInputStream()) {
//            qiniuUtils.upload2Qiniu(is, fileName);
            url = aliYunOssUtils.uploadToAliYunOSS(is, fileName);
        } catch (IOException e) {
            log.error("", e);
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }

        //写入redis
        redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).add(fileName);

        //构造返回值
        String pic = url;
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, pic);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody AddOrEditCheckMealParam addOrEditCheckMealParam) {
        log.info("[检查套餐管理-增加套餐]data:{}", addOrEditCheckMealParam);
        Setmeal checkMeal = addOrEditCheckMealParam.getCheckMeal();
        if(!StringUtils.isEmpty(checkMeal.getImg())) {

            String img = checkMeal.getImg();
            img = aliYunOssUtils.getFolder() + img.split("/")[4].split("\\?")[0];
            checkMeal.setImg(img);
        }
        checkMealService.add(addOrEditCheckMealParam.getCheckMeal(), addOrEditCheckMealParam.getCheckGroupIds());
        log.info("[检查套餐管理-增加套餐成功]data:{}", addOrEditCheckMealParam);
        //从redis中清除图片
        redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).remove(checkMeal.getImg());
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findCheckMealById")
    public Result findCheckMealById(Integer id) {
        log.info("[检查套餐管理-修改套餐回显]id={}", id);
        Setmeal checkMeal = checkMealService.findCheckMealById(id);

        String filename = checkMeal.getImg();
        String url = aliYunOssUtils.getUrlFromAliYunOss(filename);
        checkMeal.setImg(url);

        log.info("[检查套餐管理-修改套餐回显]data:{}", checkMeal);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, checkMeal);
    }

    @RequestMapping("/findCheckGroupIdByMealId")
    public Result findCheckGroupIdByMealId(Integer id) {
        log.info("[检查套餐管理-查询检查组回显id]id={}", id);
        List<Integer> checkGroupIds = checkMealService.findCheckGroupIdByMealId(id);
        log.info("[检查套餐管理-查询检查组回显]id={}", checkGroupIds);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkGroupIds);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody AddOrEditCheckMealParam mealParam) {
        log.info("[检查套餐管理-修改套餐]data:{}", mealParam);
        Setmeal checkMeal = mealParam.getCheckMeal();

        String img = checkMeal.getImg();
        img = aliYunOssUtils.getFolder() + img.split("/")[4].split("\\?")[0];
        checkMeal.setImg(img);

        checkMealService.editCheckMeal(checkMeal);
        checkMealService.editCheckGroupId(mealParam.getCheckMeal().getId(), mealParam.getCheckGroupIds());
        log.info("[检查套餐管理-修改套餐成功]");
        return new Result(true, MessageConst.EDIT_CHECKMEAL_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        log.info("[检查套餐管理-删除检查组套餐id]id={}", id);
        Setmeal meal = checkMealService.findCheckMealById(id);
        String img = meal.getImg();
        //先删除数据库信息在删除图片
        checkMealService.delete(id);
        aliYunOssUtils.deleteFileFromAliYunOSS(img);

        return new Result(true, MessageConst.DELETE_CHECKMEAL_SUCCESS);
    }
}
